<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Category;
use App\Models\Category as AppCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->column('id')->bold()->sortable();
            // $grid->column('parent_id', '大类学科')->tree();
            $grid->column('title', '学科')->tree();
            $grid->column('order')->orderable();
            $grid->column('status', '状态')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['id', 'title']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Category(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('order');
            $show->field('title');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Category(), function (Form $form) {
            $form->disableViewButton();
            $form->disableViewCheck();
            $form->display('id');
            $form->select('parent_id', trans('admin.parent_id'))
                ->options(AppCategory::selectOptions())
                ->saving(function ($v) {
                    return (int) $v;
                })->default(0)->required();
            $form->text('title');
            $form->text('order')->default(0);
            $form->hidden('status')
                ->customFormat(function ($v) {
                    return $v == 1 ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v;
                });
            $form->radio('status', '状态')->options([0 => '禁用', 1 => '正常'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
